<template>
  <div>
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="top" style="margin-left: 20px;">
      <el-row>
        <el-col :span="18">
          <el-form-item label="选择勘察记录" prop="survey">
            <el-select style="width:100%" filterable clearable v-model="temp.survey" placeholder="请选择">
              <el-option
                v-for="item in surveyOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="18" v-if="!surveyId">
          <el-form-item label="从其他施工方案复制" prop="scheme">
            <el-select style="width:100%" filterable clearable v-model="temp.scheme" placeholder="请选择">
              <el-option
                v-for="item in schemeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer">
      <el-button size="small" @click="close">取消</el-button>
      <el-button size="small" type="primary" @click="submit()">{{this.surveyId ? '修改施工方案' : '创建施工方案'}}</el-button>
    </div>
  </div>
</template>
<script>
import {getSurveyPage, getSurveyDetail, getSurveyDangerPointes, getSurveyPageInfo} from '@/api/survey/index'
import { getSchemePage } from '@/api/scheme/index'
export default {
  name: "selectSurvey",
  props:{
    surveyId:{
      type: Number
    }
  },
  data() {
    return {
      activeName: 'first',
      temp: {
        survey: this.surveyId ? this.surveyId : null,
        scheme: null
      },
      surveyOptions: [],
      surveyList: [],
      schemeOptions: [],
      schemeList: [],
      rules: {
        survey: [
          { required: true, message: '请选择勘察记录', trigger: 'blur'}
        ]
      }
    };
  },
  created() {

  },
  async mounted() {
    await this.getSurvey()
    await this.getScheme()
  },
  methods: {
    async getSurvey() {
      getSurveyPageInfo({
        pageNum:1,
        pageSize: 9999
      }).then(res=> {
        this.surveyList = res.data.list
        res.data.list.forEach(item => {
          this.surveyOptions.push({
            value: item.id,
            label: item.name
          })
        })
      })
    },
    async getScheme() {
      getSchemePage({
        pageNum:1,
        pageSize: 9999
      }).then(res=> {
        this.schemeList = res.data.list
        res.data.list.forEach(item => {
          this.schemeOptions.push({
            value: item.id,
            label: item.name
          })
        })
      })
    },
    close() {
      this.$emit('confirmSurvey')
    },
    submit() {
      this.$refs['dataForm'].validate(async (valid) => {
        if (valid) {
          // let detail = await this.getDetail(this.temp.survey)
          let surveyDetail = this.surveyList.find(item => item.id === this.temp.survey)
          let dangerPoints = []
          if(!this.surveyId) {
            dangerPoints = await this.getDangerPoints(this.temp.survey)
          }
          // let content = ''
          // detail.surveyJob.forEach(item => {
          //   content += item.content
          // })
          let info = {
            surveyId: surveyDetail.id,
            number: surveyDetail.number,
            surveyNumber: surveyDetail.number,
            name: surveyDetail.name,
            safetyNamagerId: surveyDetail.leader,
            // safetyManager: surveyDetail.leaderName,
            ownerUnit: surveyDetail.ownerUnit || '-',
            constructionUnit: surveyDetail.workUnit || '-',
            location: surveyDetail.location,
            dangerPoints: dangerPoints
            // content: content
          }
          if(this.temp.scheme) {
            info = {...info, isCopy: true, copyId: this.temp.scheme}
          }
          this.$emit('confirmSurvey', info)
        }
      })
    },
    async getDangerPoints(id) {
      return getSurveyDangerPointes({id:id}).then(res => {
        // if(res.data && res.data.lenght > 0) {
          return res.data || []
        // } else {
        //   return res.data
        // }
      }).catch(() => {
        return []
      })

    },
    async getDetail(id) {
      return getSurveyDetail({id:id}).then(res => {
        return res.data
      })
    }

  }
};
</script>
<style scoped="scoped" lang="scss">
.footer {
  margin-top: 10px;
  text-align: right;
}
</style>

